﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class LocalizationProfileTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name")]
        public void Can_CRUD(string code,
            string name)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;

            #endregion

            var profile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = code,
                Name = name,
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;

            Assert.NotNull(profile);
            profile.Code.AreEqualIgnoringCase(code);
            profile.Name.AreEqualIgnoringCase(name);
            profile.AreEqual(Processor.Execute(new GetLocalizationProfile
            {
                EntityID = profile.EntityID
            }).Result);
            profile.AreEqual(Processor.Execute(new GetLocalizationProfileByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            var upd_profile = Processor.Execute(new UpdateLocalizationProfile
            {
                EntityID = profile.EntityID,
                Code = code,
                Name = name,
                LanguageID = profile.Language.EntityID,
                TimeZoneID = profile.TimeZone.EntityID,
                CurrencyID = profile.Currency.EntityID,
                CountryID = profile.Country.EntityID,
                RegionID = profile.Region.EntityID,
                DimensionUnitID = profile.DimensionUnit.EntityID,
                WeightUnitID = profile.WeightUnit.EntityID,
                InformationUnitID = profile.InformationUnit.EntityID,
                Flags = LocalizationProfileFlags.Active
            }).Result;

            Assert.NotNull(upd_profile);
            upd_profile.AreEqual(profile);
            upd_profile.Code.AreEqualIgnoringCase(code);
            upd_profile.Name.AreEqualIgnoringCase(name);
            upd_profile.Flags.AreEqual(LocalizationProfileFlags.Active);

            Processor.Execute(new DeleteLocalizationProfile
            {
                EntityID = profile.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateLocalizationProfile
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateLocalizationProfile
            {
                Code = String.Empty
            }));
        }
    }
}
